// pages/mine/order/order.js

let {
  api_MakeOrder,
  api_addUserAddr
} = require("../../api/getData.js")

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

    goods_info:[],
    goods_cnt:0,
    color_choose:"",
    kuaidi_info:[],

    //模态框的变量
    hideModal: true, //模态框的状态  true-隐藏  false-显示
    animationData: {},

    list_name:[],
    list_tel:[],
    list_addr:[],
    kuaidi_name: "",
    kuaidi_tel: "",
    kuaidi_addr: "",
    kuaidi_messge: "",
    addr_index:0,

    disable:false,

    show_first_modal:true,
    checked:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let params1 = JSON.parse(options.goodsInfo);
    let params2 = options.goods_cnt;
    let params3 = options.color_choose;
    let params4 = JSON.parse(options.userInfo);
    let list_name_new = params4.list_name.split(",");
    let list_tel_new = params4.list_tel.split(",");
    let list_addr_new = params4.list_addr.split(",");
    let list_name_final = [];
    let list_tel_final = [];
    let list_addr_final = []
    var tmp_list = []
    for(var i=0;i< list_name_new.length;i++){
      var tmp=[];
      tmp.name = list_name_new[i];
      tmp.addr = list_addr_new[i];
      tmp.tel = list_tel_new[i];
      tmp_list.push(tmp);
      if(list_addr_new[i]!=""&&list_name_new[i]!=""&&list_tel_new[i]!=""){
        list_name_final.push(list_name_new[i]);
        list_tel_final.push(list_tel_new[i]);
        list_addr_final.push(list_addr_new[i]);
      }

    }
    this.setData({
      goods_info: params1,
      goods_cnt: params2,
      color_choose:params3,
      kuaidi_info:tmp_list,
      list_name: list_name_final,
      list_tel: list_tel_final,
      list_addr: list_addr_final,
    })
  },

  bindAddNewAddr:function(){
    var list1 = this.data.list_name;
    var list2 = this.data.list_tel;
    var list3 = this.data.list_addr;
    var addr_index = this.data.addr_index;
    if(this.data.kuaidi_name==""){
      // wx.showToast({
      //   title: '收货人不能为空',
      //   icon:'none',
      //   duration: 2000,
      // })
      wx.showModal({
        title: '错误提示',
        content: '收货人不能为空',
        showCancel: false,
        success: function (res) { }
      })
      return;
    }
    if (this.data.kuaidi_addr == "") {
      wx.showModal({
        title: '错误提示',
        content: '地址不能为空',
        showCancel: false,
        success: function (res) { }
      })
      return;
    }
    if (this.data.kuaidi_tel == "") {
      wx.showModal({
        title: '错误提示',
        content: '电话不能为空',
        showCancel: false,
        success: function (res) { }
      })
      return;
    }
    list1.push(this.data.kuaidi_name);
    list2.push(this.data.kuaidi_tel);
    list3.push(this.data.kuaidi_addr);
    var data_post = {
      "list_name":list1,
      "list_tel":list2,
      "list_addr":list3,
      "userid": app.globalData.userInfo.id
    }
    api_addUserAddr(data_post, (res) => {
      if (res.status == 200) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '修改失败',
          icon: 'fail',
          duration: 2000
        })
        return;
      }
    })
    if(this.data.checked){
      addr_index = this.data.list_name.length-1;
    }

    this.setData({
      show_first_modal:true,
      list_name:list1,
      list_tel:list2,
      list_addr:list3,
      kuadi_name: "",
      kuaidi_tel: "",
      kuaidi_addr: "",
      kuaidi_message: "",
      addr_index: addr_index,
      checked: false,
    })

  },

  bindJmp2AddNewAddr: function() {
    this.setData({
      show_first_modal: false,
    })
  },

  bindShezhiAddr: function (e) {
    this.setData({
      checked: !this.data.checked,
    })
  },

  bindShezhiAddr2: function (e) {
    this.setData({
      addr_index: e.currentTarget.dataset.index,
    })
  },

  bindKuaidi_name: function (e) {
    this.setData({
      kuaidi_name: e.detail.value,
    })
  },

  bindKuaidi_addr: function (e) {
    this.setData({
      kuaidi_addr: e.detail.value,
    })
  },

  bindKuaidi_tel: function (e) {
    this.setData({
      kuaidi_tel: e.detail.value,
    })
  },

  bindKuaidi_message: function (e) {
    this.setData({
      kuaidi_message: e.detail.value,
    })
  },

  bindMakeOrder: function (e) {
    if(this.data.disable==true){
      return;
    }else{
      this.setData({
        disable:true,
      })
    }

    var list_name = "";
    var list_addr = "";
    var list_tel = "";
    var list_kucun = "";
    for (var i = 0; i < this.data.goods_info.kucun_list_new.length; i++){
      list_kucun += this.data.goods_info.kucun_list_new[i];
      list_kucun += ";"
    }
    for(var i=0;i<this.data.list_name.length;i++){
      list_name += this.data.list_name[i];
      list_addr += this.data.list_addr[i];
      list_tel += this.data.list_tel[i];
      if(i!=this.data.list_name.length-1){
        list_name += ";"
        list_addr += ";"
        list_tel += ";"
      }
    }
    if(list_name==""||list_addr==""||list_tel==""){
      wx.showModal({
        title: '错误提示',
        content: '还未填写地址',
        showCancel: false,
        success: function (res) { }
      })
      this.setData({
        disable:false,
      })
      return;
    } 
    var data_post = {
      'kuaidi_address': this.data.list_addr[this.data.addr_index], 
      'kuaidi_name': this.data.list_name[this.data.addr_index],
      'kuaidi_tel': this.data.list_tel[this.data.addr_index],
      'kuaidi_message': this.data.kuaidi_message,
      'goodsid': this.data.goods_info.id,
      'userid': app.globalData.userInfo.id ,
      'goods_cnt':this.data.goods_cnt,
      'list_addr': list_addr,
      'list_name': list_name,
      'list_tel': list_tel,
      'list_kucun': list_kucun,

    }
    api_MakeOrder(data_post, (res) => {
      if(res.status==200){
        wx.showToast({
          title: '下单成功',
          icon: 'success',
          duration: 2000
        })
      }else if(res.status==500){
        wx.showToast({
          title: '下单失败',
          icon: 'fail',
          duration: 2000
        })
      }
      setTimeout(function () {
        wx.reLaunch({
          url: '/pages/mine/goods/goods',
        })
      }, 2000) 
    })
  },


  showModal: function (e) {
    var that = this;
    that.setData({
      hideModal: false,
    })
    var animation = wx.createAnimation({
      duration: 600,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn();//调用显示动画
    }, 200)
  },

  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function () {
      that.setData({
        hideModal: true,
        show_first_modal:true,
        kuaidi_name: "",
        kuaidi_addr: "",
        kuaidi_tel: "",
        kuaidi_message: "",
      })
    }, 720)//先执行下滑动画，再隐藏模块

  },

  //动画集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },


})